package org.spring.springboot.domain;
import org.json.JSONObject;
import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class Category {
    public Category() {
        userid = "system";
        childList = new ArrayList<>();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    private  String id;
    private  String pid;
    private  String name;
    private  String title;

    private  List<Category> childList;

    public  void addChild(Category child){
        if (childList.indexOf(child) == -1){
            childList.add(child);
        }
    }
    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    private  String userid;

    public Category(String id, String pid, String name, String title,String userid) {
        this.id = id;
        this.pid = pid;
        this.name = name;
        this.title = title;
        this.userid = userid;
        childList = new ArrayList<>();
    }

    public JSONObject toJson(){
        JSONObject obj = new JSONObject();
        obj.put("id",this.id);
        obj.put("name",this.name);
        obj.put("pid",this.pid);
        obj.put("title",this.title);
        obj.put("userid",this.userid);

        JSONArray childArr = null;
        if (childList.size() > 0){
            childArr = new JSONArray();
            for (Category child:childList) {
                JSONObject childObj = child.toJson();
                childArr.put(childObj);
            }
        }
        obj.put("children",childArr);
        return obj;
    }
}
